﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace QAToolsAddin.Components {
    public partial class ExcelColorPicker : UserControl {
        public ExcelColorPicker() {
            InitializeComponent();

            comboBox1.Items.AddRange(new object[]{
                Color.FromArgb(255, 255, 255),
                Color.FromArgb(0, 0, 0),
                Color.FromArgb(0, 51, 0),
                Color.FromArgb(51, 51, 0),
                Color.FromArgb(128, 0, 0),
                Color.FromArgb(0, 0, 128),
                Color.FromArgb(0, 128, 0),
                Color.FromArgb(51, 51, 51),
                Color.FromArgb(0, 51, 102),
                Color.FromArgb(102, 0, 102),
                Color.FromArgb(153, 51, 0),
                Color.FromArgb(255, 0, 0),
                Color.FromArgb(0, 0, 255),
                Color.FromArgb(51, 51, 153),
                Color.FromArgb(0, 255, 0),
                Color.FromArgb(128, 0, 128),
                Color.FromArgb(128, 128, 0),
                Color.FromArgb(0, 128, 128),
                Color.FromArgb(153, 51, 102),
                Color.FromArgb(0, 102, 204),
                Color.FromArgb(51, 153, 102),
                Color.FromArgb(255, 102, 0),
                Color.FromArgb(102, 102, 153),
                Color.FromArgb(153, 204, 0),
                Color.FromArgb(128, 128, 128),
                Color.FromArgb(51, 102, 255),
                Color.FromArgb(255, 153, 0),
                Color.FromArgb(150, 150, 150),
                Color.FromArgb(255, 204, 0),
                Color.FromArgb(51, 204, 204),
                Color.FromArgb(255, 0, 255),
                Color.FromArgb(255, 255, 0),
                Color.FromArgb(0, 255, 255),
                Color.FromArgb(255, 128, 128),
                Color.FromArgb(153, 153, 255),
                Color.FromArgb(192, 192, 192),
                Color.FromArgb(255, 153, 204),
                Color.FromArgb(204, 153, 255),
                Color.FromArgb(255, 204, 153),
                Color.FromArgb(153, 204, 255),
                Color.FromArgb(204, 204, 255),
                Color.FromArgb(255, 255, 153),
                Color.FromArgb(204, 255, 204),
                Color.FromArgb(255, 255, 204)
            });

        }

        private void ExcelColorPicker_Load(object sender, System.EventArgs e) {
            if (this.ParentForm is ExcelForm == false)
                throw new ArgumentException("An ExcelForm parent type is required.");
        }

        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public int SelectedColor {
            get {
                var item = (Color)comboBox1.SelectedItem;
                return (int)((item.B << 16) | (item.G << 8) | item.R);
            }
            set {
                int excelColor = (int)value;
                comboBox1.SelectedItem = Color.FromArgb(excelColor & 0xff, (excelColor >> 8) & 0xff, (excelColor >> 16) & 0xff);
            }
        }

        private void comboBox1_DrawItem(object sender, DrawItemEventArgs e) {
            if (e.Index >= 0) {
                // Get this color
                var color = (Color)comboBox1.Items[e.Index];
                // Fill background
                e.DrawBackground();
                // Draw color box
                var rect = new Rectangle { X = e.Bounds.X + 2, Y = e.Bounds.Y + 2, Width = 18, Height = e.Bounds.Height - 5 };
                e.Graphics.FillRectangle(new SolidBrush(color), rect);
                e.Graphics.DrawRectangle(SystemPens.WindowText, rect);
                // Write color name
                /*Brush brush;
                if ((e.State & DrawItemState.Selected) != DrawItemState.None)
                    brush = SystemBrushes.HighlightText;
                else
                    brush = SystemBrushes.WindowText;
                e.Graphics.DrawString(color.Text, Font, brush,
                    e.Bounds.X + rect.X + rect.Width + 2,
                    e.Bounds.Y + ((e.Bounds.Height - Font.Height) / 2));
                */
                // Draw the focus rectangle if appropriate
                if ((e.State & DrawItemState.NoFocusRect) == DrawItemState.None)
                    e.DrawFocusRectangle();
            }
        }

    }
}
